package cz.muni.fi.PA165.musiclibraryweb.signin;

import cz.muni.fi.PA165.musiclibraryapi.services.UserService;
import cz.muni.fi.PA165.musiclibrarybackend.dto.UserDTO;
import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
import org.apache.wicket.authroles.authorization.strategies.role.Roles;
import org.apache.wicket.request.Request;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Session class for signin example. Holds and authenticates users.
 * 
 */
public final class SignInSession extends AuthenticatedWebSession
{
    /** Trivial user representation */
    
    private String user;
    private boolean isAdmin;
    

    public boolean isIsAdmin() {
        return isAdmin;
    }
    
    /**
     * Constructor
     * 
     * @param request
     */
    public SignInSession(Request request)
    {
        super(request);
    }

    /**
     * Checks the given username and password, returning a User object if if the username and
     * password identify a valid user.
     * 
     * @param username
     *            The username
     * @param password
     *            The password
     * @return True if the user was authenticated
     */
    
  @Override
    public final boolean authenticate(final String username, final String password)
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = context.getBean(UserService.class);
        try {
            
            UserDTO userDTO = (UserDTO) userService.getUser(username);


            String name = userDTO.getName();
            String pass = userDTO.getPassword();


            if (name==null || pass==null){
                return Boolean.FALSE;
            }


            if (username.equals(name) && password.equals(pass)){
                this.user = username;
                if(userDTO.getAdmin()){
                    this.isAdmin = Boolean.TRUE;
                }
                else{
                    this.isAdmin = Boolean.FALSE;
                }
                return Boolean.TRUE;
            }
        
        return Boolean.FALSE;
        }
        catch(Exception e){
            return Boolean.FALSE;
        }
    }

    /**
     * @return User
     */
    public String getUser()
    {
        return user;
    }

    /**
     * @param user
     *            New user
     */
    public void setUser(final String user)
    {
        this.user = user;
    }

    /**
     * @see org.apache.wicket.authentication.AuthenticatedWebSession#getRoles()
     */
    @Override
    public Roles getRoles()
    {
        Roles roles = new Roles();
        
        if (isSignedIn())
        {
            if (isIsAdmin()){
                roles.add("ADMIN");
            }
            roles.add("USER");
        }
        return roles;
    }
}

